# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=ffmpeg
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.4.1
pkgrel=6
pkgdesc="Complete and free Internet live audio and video broadcasting solution (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://ffmpeg.org/"
license=('GPL')
options=('staticlibs' 'strip')
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-celt"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-frei0r-plugins")
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-gsm"
         "${MINGW_PACKAGE_PREFIX}-lame"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libbluray"
         "${MINGW_PACKAGE_PREFIX}-libcaca"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libgme"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libmfx"
         "${MINGW_PACKAGE_PREFIX}-libmodplug"
         "${MINGW_PACKAGE_PREFIX}-libsoxr"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencore-amr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-opus"
         $([[ "${MINGW_PACKAGE_PREFIX}" == *clang-i686* ]] || echo "${MINGW_PACKAGE_PREFIX}-rav1e")
         "${MINGW_PACKAGE_PREFIX}-rtmpdump"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-speex"
         "${MINGW_PACKAGE_PREFIX}-srt"
         $([[ "${CARCH}" != "x86_64" ]] || echo "${MINGW_PACKAGE_PREFIX}-svt-av1")
         "${MINGW_PACKAGE_PREFIX}-vulkan"
         "${MINGW_PACKAGE_PREFIX}-x264"
         "${MINGW_PACKAGE_PREFIX}-x265"
         "${MINGW_PACKAGE_PREFIX}-xvidcore"
         "${MINGW_PACKAGE_PREFIX}-zimg"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-dlfcn"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-amf-headers"
             "${MINGW_PACKAGE_PREFIX}-ffnvcodec-headers"
             "${MINGW_PACKAGE_PREFIX}-nasm")
source=(https://ffmpeg.org/releases/${_realname}-${pkgver}.tar.xz{,.asc}
        "pathtools.c"
        "pathtools.h"
        "0005-Win32-Add-path-relocation-to-frei0r-plugins-search.patch"
        "0001-get_cabac_inline_x86-Dont-inline-if-32-bit-clang-on-windows.patch")
validpgpkeys=('FCF986EA15E6E293A5644F10B4322F04D67658D8')
sha256sums=('eadbad9e9ab30b25f5520fbfde99fae4a92a1ae3c0257a8d68569a4651e30e02'
            'SKIP'
            '6f1016e6647b6340fdceefaf24ff391f4c0ea3c785ddf70c9794ca2356797888'
            '6ce4dcf4ef6c4bce48dbcb6f1b5226baf79f74ac76719fb0c06419a0aadb37a3'
            '63aa930515c10484a68cb8940f453cba01cf3f5d0d72c86b0bc03777acb704ba'
            'b19edf64c3daff6acebeb2e45be4e13c5ee035fd24ba1bf9e77a5376efe83628')

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.[ch] libavfilter/

  patch -p1 -i "${srcdir}/0005-Win32-Add-path-relocation-to-frei0r-plugins-search.patch"

  # Backport from https://github.com/FFmpeg/FFmpeg/commit/8990c5869e27fcd43b53045f87ba251f42e7d293
  patch -p1 -i "${srcdir}/0001-get_cabac_inline_x86-Dont-inline-if-32-bit-clang-on-windows.patch"
}

build() {
  # Fix using SRT headers
  CFLAGS+=" -DWIN32"
  CXXFLAGS+=" -DWIN32"

  local -a common_config
  common_config+=(
    --disable-debug
    --enable-amf
    --enable-dxva2
    --enable-d3d11va
    --enable-fontconfig
    --enable-gnutls
    --enable-gpl
    --enable-iconv
    --enable-libaom
    --enable-libass
    --enable-libbluray
    --enable-libcaca
    --enable-libcelt
    --enable-libdav1d
    --enable-libfreetype
    --enable-libgme
    --enable-libgsm
    --enable-libmfx
    --enable-libmodplug
    --enable-libmp3lame
    --enable-libopencore_amrnb
    --enable-libopencore_amrwb
    --enable-libopenjpeg
    --enable-libopus
    --enable-librtmp
    --enable-libsoxr
    --enable-libspeex
    --enable-libsrt
    --enable-libtheora
    --enable-libvorbis
    --enable-libx264
    --enable-libx265
    --enable-libxvid
    --enable-libvpx
    --enable-libwebp
    --enable-libxml2
    --enable-libzimg
    --enable-nvenc
    --enable-openal
    --enable-pic
    --enable-postproc
    --enable-runtime-cpudetect
    --enable-swresample
    --enable-version3
    --enable-vulkan
    --enable-zlib
    --disable-doc
  )

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    common_config+=(
      --enable-frei0r
    )
  fi

  if [[ "${CARCH}" == "x86_64" ]]; then
    common_config+=(
       --enable-libsvtav1
    )
  fi

  if [[ "${MINGW_PACKAGE_PREFIX}" != *clang-i686* ]]; then
    common_config+=(
       --enable-librav1e
    )
  fi

  for _variant in -static -shared; do
    [[ -d "${srcdir}/build-${MINGW_CHOST}${_variant}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}${_variant}"
    mkdir -p "${srcdir}/build-${MINGW_CHOST}${_variant}" && cd "${srcdir}/build-${MINGW_CHOST}${_variant}"
    if [[ ${_variant} == -static ]]; then
      ENABLE_VARIANT=--enable-static
    else
      ENABLE_VARIANT=--enable-shared
    fi
    ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --target-os=mingw32 \
      --arch=${CARCH%%-*} \
      --cc=${CC} \
      --cxx=${CXX} \
      "${common_config[@]}" \
      --logfile=config.log \
      ${ENABLE_VARIANT} ||
      {
        cat config.log
        exit 1
      }
    make VERBOSE=1
  done
}

check() {
  for _variant in -static -shared; do
    cd "${srcdir}/build-${MINGW_CHOST}${_variant}"
    # workaround for conflict with SDL main(), use it if you have SDL installed
    # make check CC_C="-c -Umain"
    make check VERBOSE=1 || true
  done
}

package() {
  for _variant in -static -shared; do
    cd "${srcdir}/build-${MINGW_CHOST}${_variant}"
    make DESTDIR="${pkgdir}" install
  done
  
  rm -f ${pkgdir}/${MINGW_PREFIX}/lib/*.def
  rm -f ${pkgdir}/${MINGW_PREFIX}/bin/*.lib

  #find ${pkgdir}${MINGW_PREFIX}/bin -type f -name "*.exe" -exec objcopy --subsystem console {} \;

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig -name *.pc -exec sed -i -e"s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
}
